<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="88px">
      <el-form-item label="设备名称" prop="terminalName">
        <el-input
          v-model="queryParams.terminalName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="terminalNo">
        <el-input
          v-model="queryParams.terminalNo"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属代理商" prop="agentUid">
        <el-select v-model="queryParams.agentUid" clearable>
          <el-option
            v-for="item in agentList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备状态" prop="agentUid">
         <el-select v-model="queryParams.online" clearable>
          <el-option
            v-for="item in onlineList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>

    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%;"
    >
      <el-table-column label="序号" type="index" >
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备名称"  prop="terminalName" />
      <el-table-column label="设备编号"  prop="terminalNo" />
      <el-table-column label="所属代理商"  prop="nickName" />
      <el-table-column label="总容量"  prop="capacity" >
        <template slot-scope="scope">
          <span>{{scope.row.capacity + 'L'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前剩余"  prop="currentCapacity">
        <template slot-scope="scope">
          <span>{{scope.row.currentCapacity + 'L'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="剩余百分比" >
        <template slot-scope="scope">
          <span>{{scope.row.percent * 100 + '%'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备状态" >
        <template slot-scope="scope">
          <span style="color:#3FE46D" v-show="scope.row.online == '1'">在线</span>
          <span style="color:#FF5555" v-show="scope.row.online == '0'">离线</span>
        </template>
      </el-table-column>
      <el-table-column label="预警状态"  >
        <template slot-scope="scope">
          <div v-if="scope.row.online == '1'" class="warnStatus">
            <div v-show="percent >= 5 && percent < 10" class="circle" style="background:#FAC818"></div>
            <div v-show="percent >= 0 && percent < 5" class="circle" style="background:#FF5555"></div>
            <div v-show="percent >= 10" class="circle" style="background:#3FE46D"></div>
          </div>
          <div v-else class="warnStatus">
            <div class="circle" style="background:#D0D0D0"></div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == '0'" type="text" @click="deal(scope.row)">处理</el-button>
          <span v-else>已处理</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <el-dialog
      title="终端处理"
      :visible.sync="dealShow"
      width="30%"
      top="100px">
      <div class="terminalContent">
        <div class="terminalItem">终端号：{{dealItem.terminalNo}}</div>
        <div class="terminalItem">终端名称：{{dealItem.terminalName}}</div>
      </div>
      <div class="terminalContent">
        <div class="terminalItem">终端容量：{{dealItem.capacity + 'L'}}</div>
        <div class="terminalItem">当前剩余：{{dealItem.currentCapacity + 'L'}}({{dealItem.percent*100 + '%'}})</div>
      </div>
      <div class="dealContent">
        <el-checkbox v-model="checked1" @change="checked2 = false">已加满</el-checkbox>
      </div>
      <div class="dealContent">
        <el-checkbox v-model="checked2" @change="checked1 = false">定量添加</el-checkbox>
        <el-input v-model="addNum" @input="val=>addNum=val.replace(/[^\d]/g,'')" style="flex:1;margin-left:20px" placeholder="请输入添加的数量(单位L)"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dealShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">已处理</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getNoticeList,apiHandleMsg,getAgentList } from "@/api/common";

export default {
  name: "Online",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status:'0'
      },
      onlineList:[
        {value:'1',label:'在线'},
        {value:'0',label:'离线'}
      ],
      agentList:[],
      dealShow:false,
      dealItem:{},
      checked1:false,
      checked2:false,
      addNum:null
    };
  },
  created() {
    this.getAgent()
    this.getList();
  },
  methods: {
    getAgent(){
      let req = {
        userType:'01'
      }
      getAgentList(req).then(res=>{
        if(res.code == 200){
          this.agentList = res.rows.map(e=>{
            return{
              label:e.nickName,
              value:e.userId
            }
          })
        }
      })
    },
    getList() {
      this.loading = true;
      getNoticeList(this.queryParams).then(response => {
        this.list = response.rows.map(item=>{
          return {
            ...item,
            percent:item.currentCapacity / item.capacity
          }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams =  {
        pageNum: 1,
        pageSize: 10,
        status:'0'
      }
      this.handleQuery();
    },
    deal(row){
      this.dealShow = true
      this.dealItem = row
    },
    confirm(){
      if(!this.checked1 && !this.checked2){
        this.$message.warning('请选择处理方式')
        return
      }
      if(this.checked2 && !this.addNum){
        this.$message.warning('请输入添加的数量')
        return
      }
      this.dealShow = false
      let req = {
        id:this.dealItem.id,
        noticeId:this.dealItem.noticeId,
        msgHandleType:this.checked1 ? '0' : '1',
        addNum:this.addNum,
      }
      apiHandleMsg(req).then(res=>{
        if(res.code == 200){
          this.$modal.msgSuccess("处理成功");
          this.getList();
        }
        this.reset()
      })
      
    },
    reset(){
      this.checked1 = false
      this.checked2 = false
      this.addNum = ''
    }
  }
};
</script>

<style lang="scss" scoped>
.warnStatus{
  display: flex;
  justify-content: center;
}
.circle{
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.terminalContent{
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  .terminalItem{
    width: 50%;
  }
}
.dealContent{
  display: flex;
  align-items: center;
  height: 40px;
}
</style>

